//
//  FeaturedTableView.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 7/5/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "FeaturedTableView.h"
#import "ZooSpotBean.h"
#import "DetailViewController.h"
#import "RestKit/JSONKit.h"
#import "HomeViewController.h"
#import "UIImage+Crop.h"

@implementation FeaturedTableView
@synthesize spotDetailDelegate;
@synthesize arSpot = _arSpot;

- (id)initWithFrame:(CGRect)frame object:(NSMutableArray *) pArray locatin:(ZooLocation *)pLocation {
    self = [super initWithFrame:frame];
    if (self) {
            //        _arSpot = [[NSMutableArray alloc]init];
        _arSpot = [[NSArray alloc] initWithArray:pArray];
        ZooSpotBean *zooBean = [[ZooSpotBean alloc]init];
        zooBean = [_arSpot objectAtIndex:3];
        
        _arUrlImage = [[NSMutableArray alloc]initWithObjects:@"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012_10_09-ProsciuttoArugulaPizza01.jpg",
        @"http://i136.photobucket.com/albums/q197/bermuda2810/main_dish_image-1.jpg",
        @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012-10-09-ThinCrustPizza-2.jpg",
        @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/brownie%20pie.jpeg",
        @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012_09_26-bakewell4.jpg",
        @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012_06_04-Berries.jpeg",
        @"http://i136.photobucket.com/albums/q197/bermuda2810/main_dish_image-1.jpg",
        @"http://img.foodnetwork.com/FOOD/2010/03/25/FNM_050110-Weeknight-Dinners-032_s4x3_lg.jpg",
        @"http://img.foodnetwork.com/FOOD/2011/11/14/FNM_120111-WN-Dinners-009_s4x3_lg.jpg",
        @"http://img.foodnetwork.com/FOOD/2012/05/04/FNM_060112-50-Things-to-Grill-in-Foil-Jerk-Chicken_s4x3_lg.jpg",
           @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012_10_09-ProsciuttoArugulaPizza01.jpg",
           @"http://i136.photobucket.com/albums/q197/bermuda2810/main_dish_image-1.jpg",
           @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012-10-09-ThinCrustPizza-2.jpg",
           @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/brownie%20pie.jpeg",
           @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012_09_26-bakewell4.jpg",
           @"http://i-cdn.apartmenttherapy.com/uimages/kitchen/2012_06_04-Berries.jpeg",
           @"http://i136.photobucket.com/albums/q197/bermuda2810/main_dish_image-1.jpg",
           @"http://img.foodnetwork.com/FOOD/2010/03/25/FNM_050110-Weeknight-Dinners-032_s4x3_lg.jpg",
           @"http://img.foodnetwork.com/FOOD/2011/11/14/FNM_120111-WN-Dinners-009_s4x3_lg.jpg",
           @"http://img.foodnetwork.com/FOOD/2012/05/04/FNM_060112-50-Things-to-Grill-in-Foil-Jerk-Chicken_s4x3_lg.jpg",nil];
        objMan = [[HJObjManager alloc] initWithLoadingBufferSize:20 memCacheSize:20];
        NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/ngondo/"] ;
        HJMOFileCache* fileCache = [[[HJMOFileCache alloc] initWithRootPath:cacheDirectory] autorelease];
        objMan.fileCache = fileCache;

        

        NSLog(@"%f %f",pLocation.cLatitude,pLocation.cLongitude);

        _zooLocation = pLocation;
        self.delegate = self;
        self.dataSource = self;
        [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
    }
    return self;
}

#pragma mark - Table view data source
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
//    
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        // Return the number of rows in the section.
    if (section == 1) {
        return [_arSpot count];
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MyCell";
    static NSString *CellIdentifier1 = @"MyCell1";
    
    UITableViewCell *_cell = nil;
    ListItemTableViewCell *cellView=nil;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    HJManagedImageV *hjImageV;
    
    switch (indexPath.section) {
        case 0:
            _cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];    
            if (_cell == nil) {
                _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
                [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }
            
            [self initBtnSearch:_cell];

            break;
        case 1: 
            cellView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            
            if (cellView == nil){
                cellView = [[ListItemTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
                [cellView setSelectionStyle:UITableViewCellSelectionStyleNone];
                cellView.tag = 999;
            } else {
                cellView = (ListItemTableViewCell*)[cellView viewWithTag:999];
                [cellView.vImgItem clear];
            }
            
            ZooSpotBean *spotBean = [[ZooSpotBean alloc] init];
            spotBean = [_arSpot objectAtIndex:indexPath.row];

                // calculate distance from current location to spot
            CLLocation *location = [[CLLocation alloc]initWithLatitude:[spotBean.lat floatValue] longitude:[spotBean.lon floatValue]];
            
            NSLog(@"%f %f",_zooLocation.cLatitude, _zooLocation.cLongitude);
            CLLocationDistance distance = [_zooLocation calculateDistanceFromCurrent:location];
            double distan = distance/1000;
            NSLog(@"spotBean.name = %@",spotBean.name);
            cellView.lbNameItem.text = spotBean.name;
            cellView.lbAddress.text = spotBean.address;
            cellView.lbDistance.text = [NSString stringWithFormat:@"%.2f",distan];
            cellView.vImgItem.url = [[NSURL alloc]initWithString:[_arUrlImage objectAtIndex:indexPath.row]];
            cellView.vImgItem.sizeOfimage = cellView.vImgItem.bounds.size;
            [objMan manage:cellView.vImgItem];

            _cell = cellView;
        
//            if (_cell == nil) {
//                _cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//                
//                    //Create a managed image view and add it to the cell (layout is very naieve)
//                hjImageV = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,296,75)] autorelease];
//                [hjImageV setBackgroundColor:[UIColor orangeColor]];
//                hjImageV.tag = 999;
//                [_cell addSubview:hjImageV];
//                
//            } else {
//                    //Get a reference to the managed image view that was already in the recycled cell, and clear it
//                hjImageV = (HJManagedImageV*)[_cell viewWithTag:999];
//                [hjImageV clear];
//            }
//            
//            hjImageV.url = [[NSURL alloc]initWithString:[_arUrlImage objectAtIndex:indexPath.row]];
//            hjImageV.sizeOfimage = CGSizeMake(296, 75);
//            [objMan manage:hjImageV];
            
            break;
        default:
            break;
    }

//    [location release];
//    [spotBean release];

    return _cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        ZooSpotBean *_mSpot = [[ZooSpotBean alloc] init];
        _mSpot.name = @"abc";
        
        if (spotDetailDelegate != nil) {
            [spotDetailDelegate pushToSpotDetailScreen:_mSpot];
        }
        
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    }
    return 195;
}

- (void)initBtnSearch:(UITableViewCell *)pCell {
    _btnSearch = [[UIButton alloc]initWithFrame:CGRectMake(0, 5, 298, 39)];
    _vIconSearch = [[UIImageView alloc]init ];
    _lbSearch = [[UILabel alloc]init ];

    [_btnSearch setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_SEARCH]]];
    [_vIconSearch setImage:[UIImage imageNamed:IMG_BTN_SEARCH]];
    _vIconSearch.frame = CGRectMake(8, 8, 24, 24);
    [_btnSearch addSubview:_vIconSearch];
    _lbSearch.frame = CGRectMake(45, 8, 200, 24);
    [_lbSearch setText:STRING_SEARCH_HINT];
    [_lbSearch setTextColor:[UIColor grayColor]];
    [_lbSearch setBackgroundColor:[UIColor clearColor]];
    [_lbSearch setFont:[UIFont systemFontOfSize:14]];
        //    _tfSearch.autocorrectionType = UITextAutocorrectionTypeNo;
    [_btnSearch addSubview:_vIconSearch];
    [_btnSearch addSubview:_lbSearch];
    [_btnSearch addTarget:self action:@selector(btnSearchClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [pCell.contentView addSubview:_btnSearch];
}

- (void)btnSearchClicked {
    
}
@end
